package com.tianze.tzkf.function.login;

import android.os.Bundle;
import android.support.constraint.Guideline;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.tianze.tzkf.BannerActivity;
import com.tianze.tzkf.MainActivity;
import com.tianze.tzkf.R;
import com.tianze.tzkf.base.BaseActivity;
import com.tianze.tzkf.bean.BannerListBean;
import com.tianze.tzkf.bean.CheckPwdBean;
import com.tianze.tzkf.bean.Token;
import com.tianze.tzkf.function.home.HomeActivity;
import com.tianze.tzkf.net.RetrofitFactory;
import com.tianze.tzkf.utils.ToastUtils;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Zcyang on 2018/12/29.
 */
public class CheckPwdActivity extends BaseActivity {

    private static final String TAG = CheckPwdActivity.class.toString();

    @BindView(R.id.tv_back)
    TextView tvBack;
    @BindView(R.id.et_pwd)
    EditText etPwd;
    @BindView(R.id.iv_miss_or_show)
    ImageView ivMissOrShow;
    @BindView(R.id.ll_pwd)
    LinearLayout llPwd;
    @BindView(R.id.guideline10)
    Guideline guideline10;
    @BindView(R.id.tv_ok)
    TextView tvOk;


    private boolean isShow = false;
    private BannerListBean.DataBean bannerListBean;

    @Override
    protected void initData() {
        etPwd.setText("15279603223");
 //       etPwd.setText("123456");
    }

    @Override
    protected void reflshData() {

    }

    @Override
    protected void initTitle() {
        Bundle extras = getIntent().getExtras();
        Bundle data = extras.getBundle("data");
        bannerListBean = (BannerListBean.DataBean) data.getSerializable("bannerListBean");
    }

    /**
     * 验证密码
     * @param pwd  密码
     */
    private void checkPassword(String pwd) {

        String token = readToken().getToken();

        Map<String , String> request = new HashMap<>();
        request.put("token",token);
        request.put("password",pwd);

        Observable<CheckPwdBean> login = RetrofitFactory.getInstance(TAG).checkPassword(request);
        login.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<CheckPwdBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(CheckPwdBean checkPwdBean) {
                        if (10001 == checkPwdBean.getCode()) {
                            Bundle bundle = new Bundle();
                            bundle.putSerializable("bannerListBean",bannerListBean);
                            goToActivity(HomeActivity.class,bundle);
                            removeCurrentActivity();
                        }
                            ToastUtils.showToast(CheckPwdActivity.this,checkPwdBean.getMsg());

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });


    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_check_pwd;
    }

    @Override
    protected void isNetConnect(boolean isNetChanges) {

    }



    @OnClick({R.id.tv_back, R.id.iv_miss_or_show, R.id.tv_ok})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_back:
//                finish();
                removeCurrentActivity();
                break;
            case R.id.iv_miss_or_show:
                if (!isShow) {
                    isShow = !isShow;
                    etPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance()); //如果被选中则显示密码
                    etPwd.setSelection(etPwd.getText().length());   //TextView默认光标在最左端，这里控制光标在最右端
                    Glide.with(this).load(R.drawable.pwd_show_white).into(ivMissOrShow);
                }else {
                    isShow = !isShow;
                    etPwd.setTransformationMethod(PasswordTransformationMethod.getInstance()); //如果没选中CheckBox则隐藏密码
                    etPwd.setSelection(etPwd.getText().length());
                    Glide.with(this).load(R.drawable.pwd_miss_white).into(ivMissOrShow);

                }
                break;
            case R.id.tv_ok:
                String pwd = etPwd.getText().toString().trim();
                if (TextUtils.isEmpty(pwd)) {
                    ToastUtils.showToast(CheckPwdActivity.this,"密码不能为空");
                }else {
                    checkPassword(pwd);
                }


                break;
        }
    }
}
